package xing.service.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.codegen.CompilerConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import xing.application.service.user.UserService;
import xing.domain.user.User;
import xing.infrastructure.data.CallResult;

/********************************
 * 文 件 名 :UserController.java
 * 创 建 人：陈天龙
 * 日    期：2017/10/19
 * 描   述：用户控制层
 * 版 本 号：v1.0.0
 ********************************/
@Api(value = "用户信息操作",description = "用户信息相关Api")
@RestController
@RequestMapping("/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @ApiOperation(value="用户信息新增/更新", notes="用户信息新增/更新",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(name = "user", value = "用户信息", required = true, dataType = "User")
    @PostMapping("/save")
    public CallResult saveOrUpdate(@RequestBody User user){
        CallResult cr = CallResult.ok();
        try {
            userService.addUser(user);
        } catch (Exception e) {
            logger.error("用户信息新增/更新失败",e);
            cr = CallResult.error("用户信息新增/更新失败");
        }

        return cr;
    }

//    @ApiOperation(value="用户是否存在", notes="")
//    @GetMapping("/{openId}")
//    public CallResult userExits(@PathVariable String openId){
//        CallResult res = CallResult.ok();
//        try {
//            res=userService.getUserInfo(openId);
//        } catch (Exception e) {
//            logger.error("user phone 失败",e);
//            res = CallResult.error("方法出错");
//        }
//
//        return res;
//    }
}
